/*
 * *
 *  * Created with IntelliJ RustRover.
 *  * Description:
 *  * Copyright Author: ZhenYi
 *  * DateTime: 2024/2/15 下午6:58
 *
 */


use salvo::{Depot, handler, Request, Response};
use salvo::prelude::StatusCode;
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set};
use sea_orm::prelude::Date;
use serde::{Deserialize, Serialize};
use crate::config::pgsql::userlist;

#[derive(Deserialize,Serialize)]
pub struct UserUpdateInput{
    pub id: i32,
    pub genre: Option<i32>,
    pub username: Option<String>,
    pub email: Option<String>,
    pub phone: Option<String>,
    pub name: Option<String>,
    pub sex: Option<i32>,
    pub birthday: Option<Date>,
    pub address: Option<String>,
    pub postalcode: Option<String>,
    pub jobtitle: Option<String>,
    pub companyname: Option<String>,
    pub companyaddress: Option<String>,
    pub department_faculty: Option<Option<String>>,
    pub research_direction: Option<String>,
    pub field_of_study: Option<Option<String>>,
}

#[allow(unused)]
#[handler]
pub async fn update_user_info(req: &mut Request, depot: &mut Depot, res: &mut Response){
    let input = match req.parse_json::<UserUpdateInput>().await{
        Ok(input) => input,
        Err(_) => {
            res.status_code(StatusCode::IM_USED).render("{\"Error\":\"Json parse error\"}");
            return;
        }
    };
    let db = depot.obtain::<DatabaseConnection>().unwrap();
    let id = input.id.clone();
    let ds = match userlist::Entity::find_by_id(id)
        .one(db)
        .await.unwrap(){
        None => {
            res.status_code(StatusCode::IM_USED).render("{\"Error\":\"User not found\"}");
            return;
        }
        Some(ok) => {
            ok
        }
    };
    let username = input.username.clone().unwrap_or_else(|| ds.username);
    let password =  ds.password;
    let email = input.email.clone().unwrap_or_else(|| ds.email);
    let phone = input.phone.clone().unwrap_or_else(|| ds.phone);
    let name = input.name.clone().unwrap_or_else(|| ds.name);
    let sex = input.sex.unwrap_or_else(|| ds.sex);
    let birthday = input.birthday.unwrap_or_else(|| ds.birthday);
    let address = input.address.clone().unwrap_or_else(|| ds.address);
    let postalcode = input.postalcode.clone().unwrap_or_else(|| ds.postalcode);
    let jobtitle = input.jobtitle.clone().unwrap_or_else(|| ds.jobtitle);
    let companyname = input.companyname.clone().unwrap_or_else(|| ds.companyname);
    let companyaddress = input.companyaddress.clone().unwrap_or_else(|| ds.companyaddress);
    let department_faculty = input.department_faculty.clone().unwrap_or_else(|| ds.department_faculty);
    let research_direction = input.research_direction.clone().unwrap_or_else(|| ds.research_direction);
    let field_of_study = input.field_of_study.clone().unwrap_or_else(|| ds.field_of_study);

    let user = userlist::ActiveModel{
        id: Set(id),
        genre: Set(ds.genre),
        username: Set(username),
        password: Set(password),
        email: Set(email),
        phone: Set(phone),
        name: Set(name),
        sex: Set(sex),
        birthday: Set(birthday),
        address: Set(address),
        postalcode: Set(postalcode),
        jobtitle: Set(jobtitle),
        companyname: Set(companyname),
        companyaddress: Set(companyaddress),
        department_faculty: Set(department_faculty),
        research_direction: Set(research_direction),
        field_of_study: Set(field_of_study),
    };
    match user.clone().update(db).await{
        Ok(ok) => {
            res.status_code(StatusCode::ACCEPTED).render("{\"OK\":\"OK\"}");
            return;
        }
        Err(_) => {
            res.status_code(StatusCode::IM_USED).render("{\"Error\":\"Update error\"}");
            return;
        }
    }
}